<template>
  <div class="client-detail">
    <el-page-header @back="goBack" title="返回客户管理">
      <template #content>
        <span class="page-title">客户详情</span>
      </template>
    </el-page-header>
    
    <el-card class="detail-card" v-loading="loading">
      <template #header>
        <div class="card-header">
          <span>基本信息</span>
        </div>
      </template>
      
      <el-descriptions :column="2" border>
        <el-descriptions-item label="客户ID">{{ client.clientId }}</el-descriptions-item>
        <el-descriptions-item label="客户名称">{{ client.companyName }}</el-descriptions-item>
        <el-descriptions-item label="联系人">{{ client.contactName }}</el-descriptions-item>
        <el-descriptions-item label="联系电话">{{ client.contactPhone }}</el-descriptions-item>
        <el-descriptions-item label="联系邮箱">{{ client.email || '无' }}</el-descriptions-item>
        <el-descriptions-item label="地址">{{ client.address || '无' }}</el-descriptions-item>
        <el-descriptions-item label="客户状态">
          <el-tag :type="client.creditRating === 'A' ? 'success' : 'info'">
            {{ client.creditRating === 'A' ? '活跃' : '非活跃' }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="注册时间">{{ formatDate(client.contractStart) }}</el-descriptions-item>
      </el-descriptions>
    </el-card>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import axios from 'axios'

export default {
  name: 'ClientDetail',
  setup() {
    const route = useRoute()
    const router = useRouter()
    const client = ref({})
    const loading = ref(false)
    
    const fetchClientDetail = async () => {
      const clientId = route.params.id
      if (!clientId) return
      
      loading.value = true
      try {
        const response = await axios.get(`/clients/${clientId}`)
        client.value = response.data.data
      } catch (error) {
        console.error('获取客户详情失败:', error)
        ElMessage.error('获取客户详情失败')
      } finally {
        loading.value = false
      }
    }
    
    const goBack = () => {
      router.back()
    }
    
    const formatDate = (dateString) => {
      if (!dateString) return '无'
      const date = new Date(dateString)
      return date.toLocaleDateString('zh-CN', { 
        year: 'numeric', 
        month: '2-digit', 
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit' 
      })
    }
    
    onMounted(() => {
      fetchClientDetail()
    })
    
    return {
      client,
      loading,
      goBack,
      formatDate
    }
  }
}
</script>

<style scoped>
.client-detail {
  padding: 20px;
}

.detail-card {
  margin-top: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.page-title {
  font-weight: bold;
  font-size: 18px;
}
</style>